#!/usr/bin/env python
import sys, os.path, string
dir = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.join(dir, '..', 'src'))
sys.path.insert(0, os.path.join(dir, '..', 'src', 'spiff-warehouse', 'src'))
from util       import find_db_file
from WorkflowDB import WorkflowDB

if len(sys.argv) != 2:
    print "Syntax: %s term" % sys.argv[0]
    sys.exit(1)

db          = WorkflowDB(find_db_file())
search_term = sys.argv[1]
result      = db.find_workflow('%'+search_term+'%')

for workflow in result:
    name = ''.join([c for c in workflow.get_name() if c in string.printable])
    for line in str(workflow.blob).split('\n'):
        if search_term not in line:
            continue
        line = ''.join([c for c in line if c in string.printable])
        print name + ':' + line
